package com.cn.demo4.service.impl;

import com.cn.demo4.dao.HrDao;
import com.cn.demo4.dao.impl.HrDaoImpl;
import com.cn.demo4.model.HrModel;
import com.cn.demo4.service.IHrService;
import com.cn.demo4.util.Tool;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class HrServiceImpl implements IHrService {
    @Override
    public List<HrModel> findAll(HrModel hrModel) {
        HrDao hrDao = new HrDaoImpl();
        return hrDao.findAll(hrModel);
    }

    @Override
    public String add(HrModel hrModel) {
        HrDao hrDao = new HrDaoImpl();
        int result = hrDao.add(hrModel);
        if(result ==1){
            Jedis jedis = new Jedis();
            jedis.del("hrId");
            jedis.del("hrStatus");
            jedis.del("hrHref");
            jedis.del("hrTitle");
            jedis.del("hrContentOne");
            jedis.del("hrContentTwo");
            jedis.del("hrImg");
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public int del(Integer id) {
        HrDao hrDao = new HrDaoImpl();
        int result = hrDao.del(id);
        if(result ==1){
            Jedis jedis = new Jedis();
            jedis.del("hrId");
            jedis.del("hrStatus");
            jedis.del("hrHref");
            jedis.del("hrTitle");
            jedis.del("hrContentOne");
            jedis.del("hrContentTwo");
            jedis.del("hrImg");
        }
        return result;
    }

    @Override
    public String update(HrModel hrModel) {
        HrDao hrDao = new HrDaoImpl();
        int result = hrDao.update(hrModel);
        if(result ==1){
            Jedis jedis = new Jedis();
            jedis.del("hrId");
            jedis.del("hrStatus");
            jedis.del("hrHref");
            jedis.del("hrTitle");
            jedis.del("hrContentOne");
            jedis.del("hrContentTwo");
            jedis.del("hrImg");
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public HrModel findById(Integer id) {

        HrDao hrDao = new HrDaoImpl();
        return hrDao.findById(id);
    }

    @Override
    public List<HrModel> getHrModelListForHome() {
        List<HrModel> list = new ArrayList<>();
        Jedis jedis  = new Jedis( );
        HrDao hrDao = new HrDaoImpl();
        Boolean exists = jedis.exists("hrId");
        if(exists){
            //如果id存在则拿到数据库中的数据
            List<String> hrIdList = jedis.lrange("hrId",0,-1);
            for(int i =0 ; i < hrIdList.size();i++){
                HrModel hrModel = new HrModel();
                hrModel.setId(Tool.stringToInt(jedis.lindex("hrId",i)));
                hrModel.setStatus(Tool.stringToInt(jedis.lindex("hrStatus",i)));
                hrModel.setHref(jedis.lindex("hrHref",i));
                hrModel.setTitle(jedis.lindex("hrTitle",i));
                hrModel.setContentOne(jedis.lindex("hrContentOne",i));
                hrModel.setContentTwo(jedis.lindex("hrContentTwo",i));
                hrModel.setImg(jedis.lindex("hrImg",i));
                list.add(hrModel);
            }
        }else{
            //将从数据库中拿到的数据存入缓存数据库中即redis中
            list = hrDao.getHrModelListForHome();
            for(int i = 0; i < list.size(); i++){
                jedis.rpush("hrId",Integer.toString(list.get(i).getId()));
                jedis.rpush("hrStatus",Integer.toString(list.get(i).getStatus()));
                jedis.rpush("hrHref",list.get(i).getHref());
                jedis.rpush("hrTitle",list.get(i).getTitle());
                jedis.rpush("hrContentOne",list.get(i).getContentOne());
                jedis.rpush("hrContentTwo",list.get(i).getContentTwo());
                jedis.rpush("hrImg",list.get(i).getImg());
            }
        }
        return  list;
    }

    @Override
    public int getCount(HrModel hrModel) {
        HrDao hrDao = new HrDaoImpl();
        return hrDao.getCount(hrModel);
    }
}
